#include "StdAfx.h"
#include "DisplayScheme.h"
#include "WareArithmetic.h"
#include "tinyxml.h"

CDisplayScheme::CDisplayScheme(void)
{
}

CDisplayScheme::~CDisplayScheme(void)
{
}
//----------------------------------------------------------------------------------
void CDisplayScheme::AddTexture( int32 nID, const string& strTextureName )
{
	m_mapTexture.insert( make_pair( nID, strTextureName ) );
}
//----------------------------------------------------------------------------------
const string& CDisplayScheme::GetTextureName( int32 nID )
{
	TextureNameMap::iterator iter = m_mapTexture.find( nID );
	if( iter != m_mapTexture.end() )
		return iter->second;
	return string("");
}
//////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------------
void CDisplaySchemeManager::LoadScheme( const string& strSchemeFileName )
{
	TiXmlDocument* pXMLDoc = new TiXmlDocument( strSchemeFileName.c_str() );
	pXMLDoc->LoadFile();

	TiXmlElement* pElem = NULL;
	TiXmlElement* pRootElem = pXMLDoc->RootElement();

	for( pElem = pRootElem->FirstChildElement();
		pElem != NULL; pElem = pElem->NextSiblingElement() )
	{

		string strDisplaySchemeName = pElem->Attribute("name");
		CDisplayScheme* pDisplayScheme = new CDisplayScheme();
		ReadDisplayScheme( pDisplayScheme, pElem );
		m_mapDisplayScheme.insert( make_pair( strDisplaySchemeName, pDisplayScheme ) );
	}

	SAFE_DELETE( pXMLDoc );
}
//----------------------------------------------------------------------------------
void CDisplaySchemeManager::ReadDisplayScheme( CDisplayScheme* pDisplayScheme, TiXmlElement* pElem )
{
	if( !pElem || !pDisplayScheme )
		return;

	TiXmlElement* pTextures = pElem->FirstChildElement( "Textures" );
	if( pTextures )
	{
		string strPath = pTextures->Attribute( "path" );

		for( TiXmlElement* e = pTextures->FirstChildElement(); e != NULL; e = e->NextSiblingElement() )
		{
			int32 nID = 0;
			string strID = e->Attribute( "id" );
			WareCommon::String2Int( strID, 10, nID );
			string strTextureName = e->Attribute( "name" );
			string StrTexture = strPath + "/";
			StrTexture += strTextureName;

			pDisplayScheme->AddTexture( nID, StrTexture );
		}
	}
}
//----------------------------------------------------------------------------------
CDisplayScheme* CDisplaySchemeManager::GetDisplayScheme( const string& strName )
{
	DisplaySchemeMap::iterator iter = m_mapDisplayScheme.find( strName );
	if( iter != m_mapDisplayScheme.end() )
		return iter->second;

	return NULL;
}
